<template>
  <div class="category">
    <div class="category-content">
      <div class="c-silde">
        <div
            v-for="item in categoryList"
            :key="item.id"
            @click="handleClickCategory(item)"
            :class="['c-item', item.id === activeId ? 'active' : '']">
          {{ item.name }}
        </div>
      </div>
      <div class="p-list">
<!--        产品列表-->
        <div
          class="p-item"
          v-for="item in productList"
          :key="item.id"
          @click="productDetail(item)">
          <img :src="item.image" />
          <div class="p-desc">
            <div class="p-title">{{ item.name }}</div>
            <div class="p-price-box">
              <div class="p-price">￥{{ item.price }}</div>
              <div class="p-price-box">
                <div class="add-icon" v-show="item.count" @click.stop="deleteCar(item)">-</div>
                <div class="add-num" v-show="item.count">{{ item.count }}</div>
                <div class="add-icon" @click.stop="addCar(item)">+</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      categoryList: [],
      activeId: '',
      productList: []
    }
  },
  onShow() {
    this.getCategoryList()
  },
  methods: {
    getCategoryList() {
      this.$api.categoryList().then(res => {
        this.categoryList = res
        if (res.length > 0) {
          this.activeId = res[0].id
          this.getProductList()
        }
      })
    },
    handleClickCategory(item) {
      this.activeId = item.id
      this.getProductList()
    },
    getProductList() {
      this.$api.goodsList({ category_id: this.activeId }).then(res => {
        this.productList = res.map(el => {
          return {
            ...el,
            count: 0
          }
        })
        // 更新购物车数量
        this.productList.forEach(item => {
          this.updateItemNum(item)
        })
      })
    },
    productDetail(item) {
      uni.navigateTo({
        url: `/pages/goods/detail?id=${item.id}`
      })
    },
    updateItemNum(item) {
      const carList = uni.getStorageSync('carList') || []
      const carItem = carList.find(e => e.goods_id === item.id)
      item.count = carItem ? carItem.count : 0
      this.$setCar()
    },
    addCar(item) {
      const carList = uni.getStorageSync('carList') || []
      const index = carList.findIndex(e => e.goods_id === item.id)
      if (index > -1) {
        carList[index].count++
      } else {
        carList.push({
          goods_id: item.id,
          count: 1,
          buyChecked: false,
          deleteChecked: false,
        })
      }
      uni.setStorageSync('carList', carList)
      this.updateItemNum(item)
    },
    deleteCar(item) {
      const carList = uni.getStorageSync('carList') || []
      const index = carList.findIndex(e => e.goods_id === item.id)
      if (index > -1) {
        if (carList[index].count > 1) {
          carList[index].count--
        } else {
          carList.splice(index, 1)
        }
        uni.setStorageSync('carList', carList)
      }
      this.updateItemNum(item)
    }
  }
}
</script>

<style lang="scss">
page {
  background: #f5f5f5;
}
.category {
  height: 100%;
  overflow: auto;
  padding-bottom: 60rpx;
  .p-list {
    flex-grow: 1;
    padding: 0 10rpx;
    background-color: #fff;
    .p-item {
      padding: 20rpx;
      box-sizing: border-box;
      display: flex;
      img {
        width: 80px;
        height: 80px;
      }
      .p-desc {
        flex: 1;
        padding-left: 20rpx;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .p-title {
          font-size: 30rpx;
          color: #606266;
          margin-bottom: 10rpx;
        }
        .p-price-box {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .add-num{
            padding: 0 16rpx;
          }
          .p-price {
            font-size: 30rpx;
            color: #333;
          }
          .add-icon {
            width: 40rpx;
            height: 40rpx;
            line-height: 40rpx;
            text-align: center;
            background-color: #2aac34;
            color: #fff;
            border-radius: 50%;
          }
        }
      }
    }
  }
  .category-content {
    display: flex;
    height: 100%;
    overflow: hidden;
  }
  .c-silde {
    background-color: #f8f8f8;
    width: 160rpx;
  }
  .c-item {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 52px;
    font-size: 14px;
    color: #606266;
    position: relative;
    &::before {
      content: "";
      position: absolute;
      left: 0;
      top: 50%;
      -webkit-transform: translateY(-50%);
      transform: translateY(-50%);
      height: 15px;
      width: 4px;
      opacity: .8;
      background-color: transparent;
    }
    &.active {
      color: #2aac34;
          background-color: #fff;
        &::before {
          background-color: #2aac34;
        }
     }
  }
  .top {
    background-repeat: no-repeat;
    background-size: cover;
    width: 100%;
    height: 125rpx;
    position: fixed;
    top: 0;
    z-index: 9;
    background-image: url("@/static/top.png");
    .top-title {
      position:absolute;
      top: 30rpx;
      width: 100%;
      text-align: center;
      font-size: 36rpx;
      color: #FFFFFF;
      font-weight: bold;
    }
  }
}
</style>
